<?php
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['BIB']."Objects/obj.mssql.php");	
$row = 0;
$field = '';
$numb = '';
$fi = '';
$logfile = $_FILES['file']['name'];
$handle = fopen($_FILES['file']["tmp_name"], "r");

while (($data = fgetcsv($handle, 3000, ",")) !== FALSE) {
    $row++;
	$numb[$row] = $data[0].',';
	$field[$row] = $data[0].','.$data[1].','.$data[2].','.$data[3].','.$data[4].','.$data[5].','.$data[6].','.$data[7].','.$data[8].','.$data[9].','.$data[10].',';
	$cb = $row;
	}

fclose($handle);
$Id = '';
	for ($a = 1; $a <= $cb; $a++) {
		$Id .= $numb[$a];
	}
	$ccj = strlen($Id);
	$Id = substr($Id,0,strlen($Id)-1);

	$dir = 'tmp_files/';
	$saveLocation=$dir . $logfile;
	$handle = fopen($saveLocation,"x");

	LigarMSBSD($BSD['DB01']);
		
		$Names = $GLOBALS['MSCONN']->db_array("SELECT    
			 Clientes.CliNombre + Clientes.CliApellidos AS Cliente
		FROM         
			Facturas
		INNER JOIN Clientes ON FraCliNumero = Clientes.CliNumero 
		WHERE FraCorresponsal = 305
		AND FraReferenciaCo IN ({$Id})");
		
		for ($b = 1; $b <= $Names[0]; $b++) {
		$fi .= $field[$b].$Names[$b]['Cliente']."\n";
		
		}
	fwrite($handle,$fi);
	fclose($handle);
	echo '<a href="'.$saveLocation.'">'.$logfile.'</a>';
?>